<?php
/*
 * Author :pandiam &basanas
 */
session_start();
require_once ('../BL/BL.php');
require_once ('include/functions.php');
require_once ('../Frameworks/Objects/entity.php');
require_once ('../Frameworks/Objects/category.php');

function curPageURL()
{
    $pageURL = 'http';
    if (!empty($_SERVER['HTTPS']))
    {
        if ($_SERVER['HTTPS'] == 'on')
        {
            $pageURL .= "s";
        }
    }
    $pageURL .= "://";
    if ($_SERVER["SERVER_PORT"] != "80")
    {
        $pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
    } else
    {
        $pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
    }
    return $pageURL;
}

function fixQuery($query3)
{
    if ($query3 == " " || empty($query3))
        return false;
    return true;
}

$url = curPageURL();
$queryUn = parse_url($url, PHP_URL_QUERY);
parse_str($queryUn);
$result = fixQuery($query);

/* TODO: PEN TEST THIS, AND SANITIZE THE INPUT!!!! :O */
if ($result == false)
{
    echo "<div class='block blockRed'>You have not entered a valid query...</div>";
}
else
{
    $result = BL_simpleSearch($_SESSION['uid'], $query);

    if($filter=="error"){
        $filter="both";
        UI_registerError("msg_info","No search type specified. Searched in both Categories and Entities.");
    }

    if ((empty($result[0]) || $result[0] == " ") && (empty($result[1]) || $result[1] == " "))
        echo "<div class='block blockYellow'>No results found matching your criteria...</div>";
    else
    {
        echo "<div class='block blockBlue'>";
        if ($filter == "both")
        {
            echo "<h3>Categories</h3><ul style='padding:5px 5px 10px 25px'>";
            if (count($result[0]) == 0)
            {
                echo "No categories found matching your criteria...";
            } else
            {
                foreach ($result[0] as & $cat)
                {
                    echo "<li><a href=\"browse.php?id=" . $cat->GetId() . "\">" . $cat->
                        getName() . "</a></li>";
                }
            }
            echo "</ul>";

            echo "<h3>Entities</h3><ul style='padding:5px 5px 10px 25px'>";
            if (count($result[1]) == 0)
            {
                echo "No entities found matching your criteria...";
            } else
            {
                foreach ($result[1] as & $ent)
                {
                    echo "<li><a href=\"browse.php?id=" . $ent->GetCategoryId() . "\">" . $ent->
                        getName() . " (filed under \"".BL_getCategory($ent->GetCategoryId())->getName()."\")</a></li>";
                }
            }
            echo "</ul>";
        } else
            if ($filter == "categories")
            {
                echo "<h3>Categories</h3><ul style='padding:5px 5px 10px 25px'>";
                if (count($result[0]) == 0)
                {
                    echo "No categories found matching your criteria...";
                } else
                {
                    foreach ($result[0] as & $cat)
                    {
                        echo "<li><a href=\"browse.php?id=" . $cat->GetId() . "\">" . $cat->
                            getName() . "</a></li>";
                    }
                }
                echo "</ul>";

            } else
            {
                echo "<h3>Entities</h3><ul style='padding:5px 5px 10px 25px'>";
                if (count($result[1]) == 0)
                {
                    echo "No entities found matching your criteria...";
                }
                else
                {
                    foreach ($result[1] as & $ent)
                    {
                        echo "<li><a href=\"browse.php?id=" . $ent->GetCategoryId() . "\">" . $ent->
                            getName() . " (filed under \"".BL_getCategory($ent->GetCategoryId())->getName()."\")</a></li>";
                    }
                }
                echo "</ul>";
            }
        echo "</div>";
    }
}

?>

<div name="search_results"></div>